Mathematical Operators
IDL mathematical operators are described in the following table.
Note: Also see Assignment and Compound Assignment for information on = and op= and Other Operators for information on the [ ], ( ), and ?: operators.
Note: If one or both of the operands are objects, the operator may be overloaded.
Operator |
Description |
Example |
+ |
Store the sum of 3 and 6 in B: B = 3 + 6 |
|
String Concatenation |
Store the string value of “John Doe” in B: B = 'John' + ' ' + 'Doe' |
|
List or Hash Concatenation |
Combines two lists to make a new one. list3 = list1 + list2 |
|
++ |
Adds one to the operand: A = 3 A++ PRINT, A IDL Prints: 4 Note: The increment operator supports both pre- and post-fix syntax. See Using Increment/Decrement. |
|
– |
Store the value of 5 subtracted from 9 in C: C = 9 - 5 |
|
Negation |
Change the sign of C: C = -C |
|
-- |
Subtracts one from the operand: A = 3 A-- PRINT, A IDL Prints: 2 Note: The decrement operator supports both pre- and post-fix syntax. See Using Increment/Decrement. |
|
* |
Multiplication |
Store the product of 2 and 5 in variable C: C = 2 * 5 |
Pointer dereference |
If |
|
/ |
Division |
Store result of 10.0 divided by 3.2 in variable D: D = 10.0/3.2 |
Exponentiation |
Store result of 2 raised to the 3rd power in variable B: B = 2^3 Note: How exponentiation is evaluated depends upon whether the operands are real or complex. See Using Exponentiation for details. |
|
Modulo |
I MOD J is equal to the remainder when I is divided by J. The magnitude of the result is less than that of J, and its sign agrees with that of I. Print the value of 9 modulo 5: PRINT, 9 MOD 5 IDL Prints: 4 Compute angle modulo 2p. A =(ANGLE + B) MOD (2 * !PI) |
Using Increment/Decrement
Beginning in IDL version 6.0, the increment (++) and decrement (--) operators can be applied to variables (including array subscripts or structure tags) of any numeric type. The ++ operator increments the target variable by one. The -- operator decrements the target by one. When written in front of the target variable (that is, using prefix notation), the operations are known as preincrement and predecrement, respectively. When written following the target variable (using postfix notation), they are called postincrement and postdecrement.
Note: The increment and decrement operators can only be applied to variable expressions to which a value can be assigned. Hence, the following is not allowed:A = 23++
because it attempts to apply the increment operator to a constant. Another way of stating this rule is to say that it must be possible for the expression being incremented or decremented to appear on the left-hand side of the equal sign.
The increment and decrement operators can be used either as standalone statements or within a larger enclosing expression. Although the two forms are very similar, the expression form has some efficiency and side-effect issues (described below) that do not apply to the statement form.
Increment/Decrement Statements
Increment and decrement operators can be used, along with a variable, as standalone statements:
A++
or++A
A--
or--A
The increment or decrement operator may be placed either before or after the target variable. The same operation is carried out in either case. These operators are very efficient, since the variable is incremented in place and no temporary copies of the data are made.
Increment/Decrement Expressions
Increment and decrement operators can be used within expressions. When the operator follows the target expression, it is applied after the value of the target is evaluated for use in the surrounding expression. When the operator precedes the target expression, it is applied before the value of the target is evaluated for use in the surrounding expression. For example, after executing the following statements, the value of the variable A is 27, while B is 28:
B = 27
A = B++
In contrast, after executing the following statements, both A and B have a value of 26:
B = 27
A = --B
Efficiency of Prefix vs. Postfix Operations
When used as part of an expression, the prefix form of the increment and decrement operators has an efficiency advantage over the postfix form. The reason for this is that the postfix form requires IDL to make a copy of the data, while the prefix form does not. The operations carried out by IDL to execute a prefix increment or decrement operation are:
- Fetch the target variable.
- Increment or decrement the target variable in place (no copies are made).
- Use the variable when evaluating the surrounding expression.
This is very efficient. In contrast, the postfix form requires IDL to make a copy of the variable in order to use its old value in the surrounding expression following the increment/decrement. The operations carried out by IDL to execute a postfix increment or decrement operation are:
- Fetch the target variable.
- Make a temporary copy of the variable.
- Increment or decrement the original variable.
- Use the temporary copy when evaluating the surrounding expression.
If your computation requires the postfix form, then these operations are necessary and reasonable. If not, the prefix form will use fewer resources and is the better choice. The larger the data involved, the more important this becomes. It is not a concern for small variables.
Order Of Side Effects
The way that the increment and decrement operators change the value of a variable in addition to using its value in a surrounding expression is called a side effect. In most cases, the side effects are desired, and cause no problems. Side effects can cause problems, however, if the increment or decrement operator is applied to a variable that appears more than once within a single statement or expression. Consider the following statement (taken from The C Programming Language by Brian W. Kernighan and Dennis M. Ritchie):
A[i] = i++
Which value of i is used to index A? Is it the original value of i, or the incremented value? The answer depends on the order in which the various parts of the statement are evaluated. Either answer might be considered correct, and IDL does not require one or the other. Similarly, in the statements
B = 23
A = B++ + B
the value of A could be either 47 or 46, depending on which part of the expression is evaluated first.
Note that this situation falls outside the rules of operator precedence; it is the order in which the variables themselves are evalutated that affects the result. Let’s examine the situation closely:
- Here the “old” value of B (23) is always used for the first occurrence of B in the statement.
- If the sub-statement B++ is evaluated first, the value of the second occurrence of B in the statement uses the “new” value of B (24), giving A the value 47.
- If the sub-statement that contains only the variable B is evaluated first, the “old” value of B will be used for both occurrences, and A will get the value 46.
As with most languages that implement increment and decrement operators, IDL does not require any particular ordering of evaluation within an expression in which such side effects occur. Different versions or implementations of IDL may evaluate the same expression differently. As a result, avoid writing code that depends on a particular ordering of the side effects.
Using Exponentiation
The caret (^
) is the exponentiation operator. A^
B is equal to A raised to the B power.
For real numbers, A^
B is evaluated as follows:
- If A is a real number and B is of integer type, repeated multiplication is applied.
- If both A and B are real (non-integer), the formula AB = eBlnA is evaluated.
- A0 is defined as 1.
For complex numbers, A^
B is evalutated as follows. The complex number A can be represented as A = a + ib, where a is the real part, and ib is the imaginary part. In polar form, we can represent the complex number as A = reiq = r cosθ + ir sinθ, where r cosθ is the real part, and ir sinθ is the imaginary part:
- If A is complex and B is real, the formula AB = (reiq)B = rB (cosBθ + isinBθ) is evaluated.
- If A is real and B is complex, the formula AB = eBlnA is evaluated.
- If both A and B are complex, the formula AB = eBlnA is evaluated, and the natural logarithm is computed to be ln(A) = ln(reiq) = ln(r) + iθ.